<template>
  <div class="chat-window">
    <div class="chat-header">
      <button class="history-toggle" @click="$emit('toggle-history')">
        <i class="fas fa-history"></i>
        <span>历史记录</span>
      </button>
      <h3 class="chat-title">{{ currentChat.title }}</h3>
    </div>
    
    <div class="messages-container" ref="messagesContainer">
      <div 
        v-for="message in currentChat.messages" 
        :key="message.id"
        :class="['message', message.sender]"
      >
        <div class="message-avatar">
          <i :class="message.sender === 'user' ? 'fas fa-user' : 'fas fa-robot'"></i>
        </div>
        <div class="message-content">
          <div class="message-text">{{ message.content }}</div>
          <div class="message-time">
            {{ formatTime(message.timestamp) }}
          </div>
        </div>
      </div>
      
      <div v-if="isLoading" class="loading-indicator">
        <div class="typing-animation">
          <span></span>
          <span></span>
          <span></span>
        </div>
        <span>AI正在思考...</span>
      </div>
    </div>
    
    <div class="input-container">
      <form @submit.prevent="handleSubmit" class="message-form">
        <div class="input-wrapper">
          <textarea
            v-model="messageText"
            placeholder="输入您的问题..."
            :disabled="isLoading"
            @keydown.enter.prevent="handleEnter"
            ref="textarea"
            rows="1"
          ></textarea>
          <button 
            type="submit" 
            :disabled="!messageText.trim() || isLoading"
            class="send-button"
          >
            <i class="fas fa-paper-plane"></i>
          </button>
        </div>
      </form>
    </div>
  </div>
</template>

<script>
export default {
  name: 'ChatWindow',
  props: {
    currentChat: {
      type: Object,
      required: true
    },
    isLoading: {  // 新增 isLoading prop
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      messageText: '',
    //   isLoading: false  移除本地的isLoading,使用父组件传递的
    }
  },
  watch: {
    'currentChat.messages': {
      handler() {
        this.$nextTick(() => {
          this.scrollToBottom();
        });
      },
      deep: true
    }
  },
  mounted() {
    this.scrollToBottom();
  },
  methods: {
    handleSubmit() {
      if (!this.messageText.trim() || this.isLoading) return;
      
      this.$emit('send-message', {
        content: this.messageText.trim()
      });
      
      this.messageText = '';
      this.isLoading = true;
    },
    
    handleEnter(e) {
      if (e.shiftKey) {
        // Shift+Enter 换行
        this.messageText += '\n';
      } else {
        // Enter 发送
        this.handleSubmit();
      }
    },
    
    scrollToBottom() {
      const container = this.$refs.messagesContainer;
      if (container) {
        container.scrollTop = container.scrollHeight;
      }
    },
    
    formatTime(timestamp) {
      const date = new Date(timestamp);
      return date.toLocaleTimeString('zh-CN', {
        hour: '2-digit',
        minute: '2-digit'
      });
    }
  }
}
</script>

<style scoped>
.chat-window {
  flex: 1;
  display: flex;
  flex-direction: column;
  background: rgba(15, 23, 42, 0.5);
  backdrop-filter: blur(10px);
}

.chat-header {
  padding: 20px;
  border-bottom: 1px solid #334155;
  display: flex;
  align-items: center;
  gap: 20px;
  background: rgba(30, 41, 59, 0.8);
}

.history-toggle {
  background: rgba(96, 165, 250, 0.1);
  border: 1px solid rgba(96, 165, 250, 0.2);
  color: #60a5fa;
  padding: 8px 16px;
  border-radius: 20px;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 14px;
  transition: all 0.3s;
}

.history-toggle:hover {
  background: rgba(96, 165, 250, 0.2);
}

.chat-title {
  color: #e2e8f0;
  font-size: 18px;
  font-weight: 600;
  margin: 0;
}

.messages-container {
  flex: 1;
  overflow-y: auto;
  padding: 20px;
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.message {
  display: flex;
  gap: 12px;
  max-width: 80%;
}

.message.user {
  align-self: flex-end;
  flex-direction: row-reverse;
}

.message-avatar {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  margin-top: 4px;
}

.message.user .message-avatar {
  background: linear-gradient(135deg, #3b82f6 0%, #2563eb 100%);
  color: white;
}

.message.ai .message-avatar {
  background: linear-gradient(135deg, #8b5cf6 0%, #7c3aed 100%);
  color: white;
}

.message-content {
  background: rgba(30, 41, 59, 0.6);
  border-radius: 18px;
  padding: 16px 20px;
  border: 1px solid rgba(51, 65, 85, 0.3);
  max-width: 100%;
}

.message.user .message-content {
  background: linear-gradient(135deg, rgba(59, 130, 246, 0.2) 0%, rgba(37, 99, 235, 0.2) 100%);
  border-color: rgba(59, 130, 246, 0.3);
}

.message-text {
  color: #e2e8f0;
  line-height: 1.6;
  word-wrap: break-word;
  white-space: pre-wrap;
}

.message-time {
  font-size: 12px;
  color: #94a3b8;
  margin-top: 8px;
  text-align: right;
}

.loading-indicator {
  display: flex;
  align-items: center;
  gap: 12px;
  color: #94a3b8;
  padding: 16px;
  background: rgba(30, 41, 59, 0.4);
  border-radius: 12px;
  border: 1px solid rgba(51, 65, 85, 0.3);
  align-self: flex-start;
}

.typing-animation {
  display: flex;
  gap: 4px;
}

.typing-animation span {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: #60a5fa;
  animation: typing 1.4s infinite ease-in-out;
}

.typing-animation span:nth-child(1) { animation-delay: 0s; }
.typing-animation span:nth-child(2) { animation-delay: 0.2s; }
.typing-animation span:nth-child(3) { animation-delay: 0.4s; }

@keyframes typing {
  0%, 60%, 100% { transform: translateY(0); opacity: 0.4; }
  30% { transform: translateY(-10px); opacity: 1; }
}

.input-container {
  padding: 20px;
  border-top: 1px solid #334155;
  background: rgba(30, 41, 59, 0.8);
}

.message-form {
  width: 100%;
}

.input-wrapper {
  display: flex;
  gap: 12px;
  align-items: flex-end;
  background: rgba(15, 23, 42, 0.6);
  border: 1px solid #475569;
  border-radius: 20px;
  padding: 12px 16px;
  transition: border-color 0.3s;
}

.input-wrapper:focus-within {
  border-color: #60a5fa;
  box-shadow: 0 0 0 3px rgba(96, 165, 250, 0.1);
}

textarea {
  flex: 1;
  background: none;
  border: none;
  color: #e2e8f0;
  font-size: 16px;
  resize: none;
  outline: none;
  min-height: 24px;
  max-height: 120px;
  line-height: 1.5;
  font-family: inherit;
}

textarea::placeholder {
  color: #94a3b8;
}

.send-button {
  background: linear-gradient(135deg, #3b82f6 0%, #2563eb 100%);
  border: none;
  color: white;
  width: 40px;
  height: 40px;
  border-radius: 50%;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.3s;
  flex-shrink: 0;
}

.send-button:hover:not(:disabled) {
  transform: scale(1.05);
  box-shadow: 0 4px 12px rgba(59, 130, 246, 0.3);
}

.send-button:disabled {
  background: #475569;
  cursor: not-allowed;
  transform: none;
}

@media (max-width: 768px) {
  .chat-header {
    padding: 16px;
  }
  
  .messages-container {
    padding: 16px;
  }
  
  .message {
    max-width: 90%;
  }
  
  .message-content {
    padding: 14px 16px;
  }
  
  .input-container {
    padding: 16px;
  }
  
  .history-toggle span {
    display: none;
  }
}
</style>